package com.mobilelmes;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class Login extends Activity {

	private EditText edtID, edtPW;
	private Button btnOK, btnReset;
	private String[] login;
	private static final String FILENAME="login.txt";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		FileOutputStream fout = null;
		BufferedOutputStream buffout = null;
		
		try {
			fout = openFileOutput(FILENAME, MODE_PRIVATE);
			buffout = new BufferedOutputStream(fout);
			buffout.write("mkpo\n1234\ncylam\nabcd".getBytes());
			buffout.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	edtID = (EditText)findViewById(R.id.edtID);
    	edtPW = (EditText)findViewById(R.id.edtPW);
    	btnOK = (Button)findViewById(R.id.btnOK);
    	btnReset = (Button)findViewById(R.id.btnReset);
    	btnOK.setOnClickListener(listener);
    	btnReset.setOnClickListener(listener);
        FileInputStream fin=null; 
        BufferedInputStream buffin=null;
        String fdata="";
        try {
			fin=openFileInput(FILENAME);
			buffin=new BufferedInputStream(fin);
			byte[] buffbyte=new byte[20];
			fdata="";
			do { 
				int flag=buffin.read(buffbyte);
				if(flag==-1)  break;
				else fdata += new String(buffbyte);
			}while(true);
			buffin.close();
			}	catch(Exception e)	{
				e.printStackTrace();
			}
        login=fdata.split("\n");

	}
	private Button.OnClickListener listener=new Button.OnClickListener() {  
        public void onClick(View v)
        {
          switch(v.getId())
          {
            case R.id.btnOK:
            	if(edtID.getText().toString().equals("") || edtPW.getText().toString().equals("")){
            		Toast.makeText(getApplicationContext(), "All fields must be filled", Toast.LENGTH_LONG) .show();
            		break;
            	}
            	Boolean flag=false;
            	for(int i=0;i<login.length;i+=2){
            		if(edtID.getText().toString().equals(login[i])){
            			flag=true;
            			if(edtPW.getText().toString().equals(login[i+1])){
            				new AlertDialog.Builder(Login.this)
			            	.setTitle("Welcome")
			        		.setMessage("Login Success")
			        		.setPositiveButton("Ok", new DialogInterface.OnClickListener()
			        		{
			        			public void onClick(DialogInterface dialoginterface, int i) {
			        	    		Intent intent=new Intent();
			        	    		intent.setClass(Login.this,Main.class);
			        	    		startActivity(intent);
			        			}
			        		})
			        		.show();
            			} else {
                    		Toast.makeText(getApplicationContext(), "Wrong Password", Toast.LENGTH_LONG) .show();
                    		edtPW.setText("");
                    		break;
                		}
            		}
            	}
            	if(!flag) {
	        		Toast.makeText(getApplicationContext(), "Wrong ITSC", Toast.LENGTH_LONG) .show();
	        		edtID.setText("");
	        		edtPW.setText("");
            	}
	        	break;
            case R.id.btnReset:  
        		edtID.setText("");
        		edtPW.setText("");
                break;
           }
        }
 	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class LoginFragment extends Fragment {

		public LoginFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_login, container,
					false);
			return rootView;
		}
	}

}
